package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.query.RegionQueryObject;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by admin on 2019/8/8.
 */
@Controller
@RequestMapping("/region")
public class RegionController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    // 查询地区列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQueryObject qo) {
        model.addAttribute("pageInfo", regionService.query(qo));
        return "/region/list";
    }

    // 查询关联目的地
    @GetMapping("/getDestByDeep")
    @ResponseBody
    public List<Destination> getDestByDeep(int deep) {
        return destinationService.queryForList(deep);
    }

    // 新增或修改地区
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Region region) {

        try {
            regionService.saveOrUpdate(region);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "保存失败");
        }
        return new AjaxResult(true);
    }

    // 删除地区
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {

        try {
            regionService.deleteRegionById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
        return new AjaxResult(true);
    }

    // 修改地区热门状态
    @GetMapping("/changeHotValue")
    @ResponseBody
    public AjaxResult changeHotValue(int hot, Long id) {

        try {
            regionService.changeHotValueById(hot, id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败");
        }
        return new AjaxResult(true);
    }

    // 查看关联地区
    @GetMapping("/getDestByRegionId")
    @ResponseBody
    public List<Destination> getDestByRegionId(Long rid) {
        return regionService.getDestByRegionId(rid);
    }
}
